import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/component/list/table_view.dart';
import 'package:industry_net/pages/news/main/models/comment.dart';
import 'package:industry_net/pages/news/news_detail/widgets/cell.dart';
import 'package:industry_net/pages/news/news_detail/widgets/comment_bar/comment_bar.dart';

import 'index.dart';
import 'widgets/widgets.dart';

showReplyPage({required NewsCommentModel md}) {
  // Get.bottomSheet(
  //   Padding(
  //     padding: EdgeInsets.only(top: SizeFit.statusBarHeight),
  //     child: NewsReplyPage(
  //       md: md,
  //     ),
  //   ),
  //   isScrollControlled: true,
  //   backgroundColor: Colors.transparent,
  //   // constraints: BoxConstraints.expand(
  //   //     width: Get.width, height: Get.height - SizeFit.statusBarHeight),
  // );
  showModalBottomSheet(
      context: Get.context!,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      constraints: BoxConstraints.expand(
          width: Get.width, height: Get.height - SizeFit.statusBarHeight),
      builder: (_) {
        return NewsReplyPage(
          md: md,
        );
      });
}

class NewsReplyPage extends StatelessWidget {
  NewsReplyPage({Key? key, required this.md}) : super(key: key);
  // NewsReplyPage({Key? key}) : super(key: key);
  final controller = Get.put(NewsReplyController());
  final NewsCommentModel md;
  // 内容页
  Widget _buildView(bool isEnableScroll) {
    var _itemkey = GlobalKey();
    return TableView(
        controller: controller.state.scrollCtrl,
        physics: isEnableScroll
            ? const NeverScrollableScrollPhysics()
            : const ClampingScrollPhysics(),
        header: Column(
          children: [
            Padding(
              padding: EdgeInsets.fromLTRB(gap12, gap15, gap12, 0),
              child: NewsCommentItemView(
                key: GlobalKey(),
                md: md,
                callback: controller.actionCellClick,
                onPraise: controller.actionPraise,
              ),
            ),
            Container(
              height: 10.r,
              color: ColorsUtil.separator,
            ),
          ],
        ),
        data: controller.state.dataSource);
  }

  @override
  Widget build(BuildContext context) {
    controller.state.md = md;

    return GetBuilder<NewsReplyController>(
      builder: (_) {
        return StreamBuilder<double>(
          stream: controller.state.streamCtrl.stream,
          initialData: controller.state.totalHeight,
          builder: (context, snapshot) {
            double currentHeight =
                snapshot.data ?? controller.state.totalHeight;
            return ClipRRect(
              borderRadius: BorderRadius.vertical(
                top: Radius.circular(25.r),
              ),
              child: AnimatedContainer(
                color: Colors.white,
                duration: const Duration(milliseconds: 30),
                height: currentHeight,
                child: Listener(
                  onPointerMove: (event) {
                    if (controller.controller.state.focusNode.hasFocus) {
                      controller.controller.state.focusNode.unfocus();
                    }
                    // 触摸事件过程 手指一直在屏幕上且发生距离滑动
                    if (controller.state.scrollCtrl.offset != 0) {
                      // 只有列表滚动到顶部时才触发下拉动画效果
                      return;
                    }
                    double distance =
                        event.position.dy - controller.state.beginOff;
                    if (distance.abs() > 0) {
                      // 获取手指滑动的距离，计算弹框实时高度，并发送事件
                      double _currentHeight =
                          controller.state.totalHeight - distance;
                      if (_currentHeight > controller.state.totalHeight) {
                        return;
                      }
                      controller.state.streamCtrl.sink.add(_currentHeight);
                    }
                  },
                  onPointerUp: (event) {
                    // 触摸事件结束 手指离开屏幕
                    // 这里认为滑动超过一半就认为用户要退出了，值可以根据实际体验修改
                    if (currentHeight < (controller.state.totalHeight * 0.8)) {
                      Navigator.pop(context);
                    } else {
                      controller.state.streamCtrl.sink
                          .add(controller.state.totalHeight);
                    }
                  },
                  onPointerDown: (event) {
                    // 触摸事件开始 手指开始接触屏幕
                    controller.state.beginOff = event.position.dy +
                        controller.state.scrollCtrl.offset +
                        ScreenUtil().statusBarHeight;
                  },
                  child: Scaffold(
                    backgroundColor: Colors.white,
                    appBar: AppBar(
                      automaticallyImplyLeading: false,
                      elevation: 0,
                      flexibleSpace: NewsReplyNavBar(),
                    ),
                    body: Container(
                      color: ColorsUtil.white,
                      padding:
                          EdgeInsets.only(bottom: SizeFit.safeBottom + 50.r),
                      child: _buildView(
                          currentHeight != controller.state.totalHeight),
                    ),
                    floatingActionButtonLocation:
                        FloatingActionButtonLocation.centerDocked,
                    floatingActionButton: NewsCommentBar(
                      controller: controller.controller,
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }
}
